import logging
import time

import config
import requests


def getUser(user):
    for account in config.users:
        if (account[0] == user):
            return account[0], account[1], account[2]


# 登录信息
hash_var = {} 

def getHeaders(account):
    """用户登录并获取请求头

    Args:
        account (_type_): _description_

    Returns:
        _type_: _description_
    """
    if account in hash_var:
        header = hash_var[account]
        header['timestamp'] = str(time.time())
        
        return header
    else:
        # 使用默认的headerJson
        header = config.headersJson.copy()
        username, password, mac = getUser(account)
        
        # 发起登录请求
        url = f'http://erp.fangline.cn/login/login-by-account-PC.json?mac={mac}&extMpAppid=&tel={username}&password={password}'
        logging.info(f"{account} 登录请求:{url}")
        response = requests.post(url, headers=config.headersJson)

        # 登录成功，获取响应头中的Set-Cookie数据
        set_cookie = response.headers.get("Set-Cookie")
        set_cookie += ';'
        
        header['cookie'] = set_cookie
        header['timestamp'] = str(time.time())
        
        hash_var[account] = header
        
        return header
        